package com.deram.ctin.serv.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.common.consts.ErrorConsts;
import com.deram.ctin.common.error.BusinessException;
import com.deram.ctin.common.utils.ArrayHandleUtils;
import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.serv.utils.MybatisUtils;
import com.deram.ctin.serv.business.entity.BaseUpdateInfo;
import com.deram.ctin.serv.business.entity.BusinessInfo;
import com.deram.ctin.serv.business.entity.BusinessInvestment;
import com.deram.ctin.serv.business.entity.BusinessJudgement;
import com.deram.ctin.serv.business.filter.BusinessInvestmentFilter;
import com.deram.ctin.serv.business.filter.BusinessJudgementFilter;
import com.deram.ctin.serv.business.mapper.BusinessInvestmentMapper;
import com.deram.ctin.serv.business.service.BusinessInfoService;
import com.deram.ctin.serv.business.service.BusinessInvestmentService;
import com.deram.ctin.serv.enter.api.EnterInfoApi;
import com.deram.ctin.serv.enter.entity.EnterInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jtt
 * @version 1.0, 2017/9/28
 */
@Service
public class BusinessInvestmentServiceImpl implements BusinessInvestmentService {

    @Autowired
    BusinessInvestmentMapper businessInvestmentMapper;

    @Autowired
    BusinessInfoService businessInfoService;

    @Autowired
    private EnterInfoApi enterInfoApi;

    @Override
    public BusinessInvestment find(Long id) {
        ValidateUtils.isId(id, "对外投资ID");
        BusinessInvestment result = businessInvestmentMapper.selectById(id);
        BusinessUtils.notNull(result, "对外投资信息不存在");
        return result;
    }

    @Override
    public List<BusinessInvestment> list(BusinessInvestmentFilter filter) {
        ValidateUtils.isId(filter.getEnterId(), "企业ID");
        EntityWrapper<BusinessInvestment> ew = createEntityWrapper(filter);
        List<BusinessInvestment> result = businessInvestmentMapper.selectList(ew);
        return result;
    }

    @Override
    public BusinessInvestment save(BusinessInvestment param) {
        ValidateUtils.isId(param.getEnterId(), "企业ID");
        if (null != param.getId()) {
            BusinessInvestment finder = find(param.getId());
            businessInvestmentMapper.updateById(param);
        } else {
            businessInvestmentMapper.insert(param);
        }
        return param;
    }

    @Override
    public void delete(Long id) {
        businessInvestmentMapper.deleteById(find(id));
    }

    @Override
    public void deleteByEnterId(Long id) {
        BusinessInvestmentFilter filter = new BusinessInvestmentFilter();
        filter.setEnterId(id);
        List<BusinessInvestment> lists = list(filter);
        if (null != lists && !lists.isEmpty()) {
            List<Long> ids = ArrayHandleUtils.readAttrList(lists, "id");
            businessInvestmentMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public void saveList(BaseUpdateInfo<BusinessInvestment> updateInfo) {
        Long enterId = updateInfo.getEnterId();
        BusinessInfo business = businessInfoService.find(enterId);
        if (null == business) {
            throw new BusinessException(ErrorConsts.VALIDATE, "工商信息未找到");
        }
        deleteByEnterId(enterId);
        List<BusinessInvestment> lists = updateInfo.getList();
        for (BusinessInvestment item : lists) {
            item.setId(null);
            item.setEnterId(enterId);
        }
        businessInvestmentMapper.batchInsert(lists);
    }

    @Override
    public PageResult<BusinessInvestment> searchWithEnter(BusinessInvestmentFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo,pageSize);
        EntityWrapper<BusinessInvestment> entityWrapper = createEntityWrapper(filter);
        PageResult<BusinessInvestment> pageResult = MybatisUtils.createPageResult(businessInvestmentMapper.selectPage(pagination,entityWrapper),pagination);
        for (BusinessInvestment obj : pageResult.getList()) {
            EnterInfo enterInfo = enterInfoApi.findById(obj.getInvestId());
            obj.setInvestEnterInfo(enterInfo);
        }
        return pageResult;
    }

    private EntityWrapper<BusinessInvestment> createEntityWrapper(BusinessInvestmentFilter filter) {
        BusinessInvestment entity = new BusinessInvestment();
        entity.setEnterId(filter.getEnterId());
        entity.setYear(filter.getYear());
        EntityWrapper<BusinessInvestment>  ew = MybatisUtils.createEntityWrapper(entity, filter);
        if(null != filter.getHasYear()){
            if(filter.getHasYear()){
                ew.isNotNull("year");
            } else {
                ew.isNull("year");
            }
        }
        return ew;
    }
}
